package com.patterns.singleton;

/**
 * 懒汉式单例模式
 * 延迟初始化，当第一次调用时才初始化实例
 * 资源利用率高
 * 但每次调用时都要同步，并发效率低
 * 注意getInstance()时的synchronized同步关键字
 * @author huangke
 *
 */
public class SingletonDemo2 {
	private  static SingletonDemo2 instance;
	
	private SingletonDemo2(){
		
	}
	
	public static synchronized SingletonDemo2 getInstance(){
		if(null == instance){
			instance = new SingletonDemo2();
		}
		return instance;
	}
}

class Test2{
	public static void main(String[] args) {
		SingletonDemo2 demo = SingletonDemo2.getInstance();
	}
}
